#!/bin/sh
#
# This is part of OpenIPC.org project | 2020.08.01
#
# ar0237 imx290 imx307 imx323 imx385 jxf22 ov2718 ov2718_2a sc2235


# SoC detect
chipid=$(ipcinfo --chip-name)

# MMZ config
mem_start=0x80000000;                             # phy mem start

mem_total=$(fw_printenv -n totalmem | tr -d 'M')
mem_total=${mem_total:=64}

os_mem_size=$(fw_printenv -n osmem | tr -d 'M')
os_mem_size=${os_mem_size:=32}

report_error()
{
        echo "******* Error: There's something wrong, please check! *****"
        exit 1
}

insert_mmz()
{
}

insert_detect()
{
        insert_mmz
        insert_isp
        SENSOR=ar0130 insert_sns
}

remove_detect()
{
}

remove_sns()
{
        rmmod -w sensor_spi &> /dev/null
        rmmod -w sensor_i2c &> /dev/null
}

insert_isp()
{
}

insert_sns()
{
}

insert_ko()
{
	find . -maxdepth 1 -type f -name "sensor_*.ko" -exec insmod {} \;
	insmod akcamera.ko
}

remove_ko()
{
}

calc_mmz_info()
{
        mmz_start=`echo "$mem_start $os_mem_size" | 
        awk 'BEGIN { temp = 0; }
        {
                temp = $1/1024/1024 + $2;
        } 
        END { printf("0x%x00000\n", temp); }'`

        mmz_size=`echo "$mem_total $os_mem_size" | 
        awk 'BEGIN { temp = 0; }
        {
                temp = $1 - $2;
        } 
        END { printf("%dM\n", temp); }'`
        echo "mmz_start: $mmz_start, mmz_size: $mmz_size"
}


######################parse arg###################################
b_arg_os_mem=0
b_arg_total_mem=0
b_arg_sensor=0
b_arg_insmod=0
b_arg_remove=0
b_arg_online=1

for arg in $@
do
        if [ $b_arg_total_mem -eq 1 ]; then
                b_arg_total_mem=0;
                mem_total=$arg;

                if [ -z $mem_total ]; then
                        echo "[error] mem_total is null"
                        exit;
                fi
        fi

        if [ $b_arg_os_mem -eq 1 ] ; then
                b_arg_os_mem=0;
                os_mem_size=$arg;

                if [ -z $os_mem_size ]; then
                        echo "[error] os_mem_size is null"
                        exit;
                fi
        fi

        if [ $b_arg_sensor -eq 1 ] ; then
                b_arg_sensor=0
                SENSOR=$arg;
        fi

        case $arg in
                "-i")
                        b_arg_insmod=1;
                        ;;
                "-r")
                        b_arg_remove=1;
                        ;;
                "-a")
                        b_arg_insmod=1;
                        b_arg_remove=1;
                        ;;
                "-h")
                        load_usage;
                        ;;
                "-sensor")
                        b_arg_sensor=1;
                        ;;
                "-osmem")
                        b_arg_os_mem=1;
                        ;;
                "-total")
                        b_arg_total_mem=1;
                        ;;
                "-offline")
                        b_arg_online=0;
                        ;;
        esac
done
#######################parse arg end########################

if [ $os_mem_size -ge $mem_total ] ; then
        echo "[err] os_mem[$os_mem_size], over total_mem[$mem_total]"
        exit;
fi

calc_mmz_info;

#######################Action###############################

if [ $# -lt 1 ]; then
    load_usage;
    exit 0;
fi

# Sensor config
# SENSOR=${SENSOR:=imx307}
#

if [ -n "$SENSOR" ]; then
        logger -s -p daemon.info -t anyka "Manualy set SENSOR as ${SENSOR}"
else
        if fw_printenv -n sensor >/dev/null; then
                SENSOR_ENV=$(fw_printenv -n sensor)
                export SENSOR=${SENSOR_ENV}
                logger -s -p daemon.info -t anyka "Get data from environment and set SENSOR as ${SENSOR}"
        else
                #insert_detect
                SENSOR_DETECT=$(ipcinfo --short-sensor)
                export SENSOR=${SENSOR_DETECT:=unknown}
                #remove_detect
                logger -s -p daemon.info -t anyka "Get data from ipcinfo and set SENSOR as ${SENSOR}"
                fw_setenv sensor $SENSOR && logger -s -p daemon.info -t anyka "Write detected ${SENSOR} to U-Boot ENV"
        fi
fi

if [ $b_arg_remove -eq 1 ]; then
        remove_ko;
fi

cd /lib/modules/3.4.35/anyka

if [ "$SENSOR" = "unknown" ]; then
        exit 1
else
        if [ $b_arg_insmod -eq 1 ]; then
                insert_ko;
        fi
fi
